﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using baseSet.DAL;
using System.Data.SqlClient;
using baseSet.csBulider;

namespace baseSet.csBulider
{
    public partial class FrmCRUDclass : Form
    {
        StringBuilder str = new StringBuilder();
        public FrmCRUDclass()
        {
            InitializeComponent();
        }

        private void buttonCreater_Click(object sender, EventArgs e)
        {

            str.AppendLine("    public int "+ txtTableName.Text.ToString()+"Insert(orderStockReturnMasterModel row)");
            str.AppendLine("        {");
            str.AppendLine("               SqlCommand cmd = new SqlCommand();");
            str.AppendLine("               cmd.Connection = conn;");
            str.AppendLine("               cmd.CommandType = CommandType.Text;");
            str.AppendLine("                string insertStr = dhDataAcess.baseBLL.dTableColumnRead.insertSqlStr(" + txtTableName.Text.ToString() + ").ToString();");

            SqlConnection con = dataConn.connOpen();
            SqlTransaction tran = con.BeginTransaction();
            
            SqlDataReader reader = readDataTabelColumn.readTableByfieldList(txtTableName.Text.ToString());
            str.AppendLine("                SqlParameter[] para = {");
            string tempstr = null;
            while (reader.Read())
            {
               tempstr="                            new SqlParameter (\"@"+reader[0].ToString()+"\",row."+reader[0].ToString()+"),";
               str.AppendLine(tempstr);
            }

            str.AppendLine("                                        }");
            str.AppendLine("            cmd.Parameters.AddRange(para);");
            str.AppendLine("            cmd.CommandText = insertStr;");
            str.AppendLine("            cmd.Transaction = tran;");
            str.AppendLine("            int i = cmd.ExecuteNonQuery();");
            str.AppendLine("            return i;");
            str.AppendLine("        }");

            str.AppendLine("    public int " + txtTableName.Text.ToString() + "Update(orderStockReturnMasterModel row)");
            str.AppendLine("        {");
            str.AppendLine("               SqlCommand cmd = new SqlCommand();");
            str.AppendLine("               cmd.Connection = conn;");
            str.AppendLine("               cmd.CommandType = CommandType.Text;");
            str.AppendLine("                string updateStr = dhDataAcess.baseBLL.dTableColumnRead.updateSqlStr(" + txtTableName.Text.ToString() + ").ToString();");


            str.AppendLine("     }");
            str.AppendLine("}");
            string s = str.ToString();
            richTxtContent.Text = s ;
        }

        private void buttonReaderTxt_Click(object sender, EventArgs e)
        {
            openFileDialogTxt.ShowDialog();
        }

        private void openFileDialogTxt_FileOk(object sender, CancelEventArgs e)
        {
            List<string> codeStringList = readerTxt.readCsv(openFileDialogTxt.FileName);
            foreach (string s in codeStringList)
            {
                str.AppendLine(s);
            }
            richTxtContent.Text = str.ToString();
        }

        private void buttonWrite_Click(object sender, EventArgs e)
        {
            saveFileDialogCS.ShowDialog();
        }

        private void saveFileDialogCS_FileOk(object sender, CancelEventArgs e)
        {
            string wrintS = richTxtContent.Text;
            string[] cc = { "\n" };
            string[] lines = wrintS.Split(cc, StringSplitOptions.None);

            System.IO.File.WriteAllLines(saveFileDialogCS.FileName, lines, Encoding.UTF8);
            MessageBox.Show("写入成功");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("是否清空代码？", "message", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                str = new StringBuilder();
                richTxtContent.Text = "";
            }
           
            //MessageBox.Show(wrintS);
        }

        private void FrmCRUDclass_Load(object sender, EventArgs e)
        {
            SqlDataReader reader = readDataTabelColumn.readTableList();
            while (reader.Read())
            {
                litstTableList.Items.Add(reader[0].ToString());
            }
        }

        private void litstTableList_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtTableName.Text = litstTableList.Text.ToString();
        }
    }
}
